package com.example.fiction.utils;

import com.example.fiction.app.MyApplication;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * 读取assets文件
 * author: 姚鸿杰
 * time: 2019/4/21 11:28
 **/
public class ReadAssets {

    public static String readPathText(String path) throws IOException {
        InputStream is = MyApplication.getContext().getResources().getAssets().open(path);
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String readLine = null;
        StringBuilder sb = new StringBuilder();
        while ((readLine = br.readLine()) != null) {
            if (readLine.charAt(0) == '-') {
                continue;
            } else {
                sb.append(readLine);
                sb.append('\r');
            }
        }
        return sb.toString();
    }

    public static String[] analysisTextException(String path, String key) throws IOException, JSONException {
        JSONObject json = new JSONObject(readPathText(path));
        String code = json.getString(key);
        String msg = json.getString(code);
        return new String[]{code, msg};
    }

}
